package codec2;

import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

//管道处理器 通道读写 管道处理业务
public class NettyServerHander extends SimpleChannelInboundHandler<MyDataInfo.MyMessage> {

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, MyDataInfo.MyMessage myMessage) throws Exception {
        MyDataInfo.MyMessage.DataType dataType = myMessage.getDataType();
        switch (dataType){
            case studentType:
                System.out.println("student id：" + myMessage.getStudent().getId() + "  name：" + myMessage.getStudent().getName());
                break;
            case teacherType:
                System.out.println("teacher id：" + myMessage.getTeacher().getId() + "  name：" + myMessage.getTeacher().getName());
                break;
            default:
                break;
        }
    }

    //数据读取完毕
    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        //数据写入缓存，并刷新
        ctx.writeAndFlush(Unpooled.copiedBuffer("hello,客户端，读取完毕。", CharsetUtil.UTF_8));

    }

    //处理异常

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        ctx.close();
    }



}
